var deputeID = GetQueryString("id");
//发送请求获取委托单基本信息
var sentMessage = {
    "requestType": "initBaseData",
    "task_id": deputeID
};
var postData;
$.ajax({
    url: '/task/checkDepute',
    type: 'post',
    data: sentMessage,
    async: false,
    success: function (data, status) {
        if (status == 'success') {
            postData = data;
            // window.location.href = '/projectManage.html';
        }

    },
    error: function (data, status) {
        if (status == 'error') {
            alert("你的网络有问题！");
        }
    }
});
if (authority[4] === 1 && GetQueryString("h") == "false") {
    document.getElementById("back").setAttribute("style", "width: 50%; float: left; text-align: center");
    document.getElementById("allocate").setAttribute("style", "width: 50%; float: left; text-align: center");
}
console.log(postData);
//显示基本信息
var basicInfomation = document.getElementById("infomation").children[0];
var basicInfomationData = [
    ["委托单位", "项目名称", "产品名称", "检测时机", "检测地点", "检测数量", "检测标准", "产品编号", "检测时间", "委托人", "联系电话", "验收标准", "委托说明"],
    []];
basicInfomationData[1] = [
    postData[0].task_company,
    postData[0].task_pname,
    postData[0].task_product,
    postData[0].task_opportunity,
    postData[0].task_addr,
    postData[0].task_workNum,
    postData[0].task_standard,
    postData[0].task_pdid,
    postData[0].task_time,
    postData[0].user_name,
    postData[0].task_phone,
    postData[0].task_check,
    postData[0].task_description
];
for (let i = 0; i < basicInfomationData[0].length; i++) {
    let tr = document.createElement("tr");
    if (i % 3 === 0) {
        basicInfomation.appendChild(tr);
    }
    let td = document.createElement("td");
    td.setAttribute("class", "td-shadows");
    td.setAttribute("align", "center");
    td.innerHTML = basicInfomationData[0][i];
    basicInfomation.children[basicInfomation.childElementCount - 1].appendChild(td);
    td = document.createElement("td");
    td.setAttribute("align", "center");
    td.innerHTML = basicInfomationData[1][i];
    basicInfomation.children[basicInfomation.childElementCount - 1].appendChild(td);
}

//发送请求获取工件信息
sentMessage = {
    "requestType": "initWorkPieceData",
    "w_tid": deputeID
};
$.ajax({
    url: '/task/checkDepute',
    type: 'post',
    data: sentMessage,
    async: false,
    success: function (data, status) {
        if (status == 'success') {
            postData = data;
            // window.location.href = '/projectManage.html';
        }

    },
    error: function (data, status) {
        if (status == 'error') {
            alert("你的网络有问题！");
        }
    }
});
console.log(postData);
//显示工件信息
var workpieceTable = document.getElementById("workpieceTable");
var workpieceInfomation = [];
for (let i = 0; i < postData.length; i++) {
    workpieceInfomation[i] = [
        postData[i].w_pipeNum,
        postData[i].w_weldNum,
        postData[i].w_pipeName,
        postData[i].w_scale,
        postData[i].w_spec,
        postData[i].w_texture,
        postData[i].w_type,
        postData[i].w_weldM,
        postData[i].w_weldPer,
        postData[i].w_weldTime,
        postData[i].w_test,
        postData[i].w_qua
    ];
}
for (let i = 0; i < workpieceInfomation.length; i++) {
    let tr = document.createElement("tr");
    let inner = "";
    for (let j = 0; j < workpieceInfomation[0].length; j++) {
        inner += "<td>" + workpieceInfomation[i][j] + "</td>";
    }
    tr.innerHTML = inner;
    workpieceTable.children[1].appendChild(tr);
}

var part_id;
var partName = [];
function OnAllocate() {
    $.ajax({
        url: '/user/department',
        type: 'post',
        data: {
            "requestType": "list"
        },
        async: false,
        success: function (data, status) {
            if (status == 'success') {
                postData = data;
                console.log(postData)
                // window.location.href = '/projectManage.html';
            }

        },
        error: function (data, status) {
            if (status == 'error') {
                alert("你的网络有问题！");
            }
        }
    });
    for (var i = 0; i < postData.length; i++) {
        partName.push([postData[i].part_name, postData[i].part_id, postData[i].part_m]);
    }
    var teamOption = document.getElementById("groupOption");
    for (let i = 0; i < partName.length; i++) {
        let option = document.createElement("option");
        option.innerHTML = partName[i][0];
        teamOption.appendChild(option);
    }
    document.getElementById("chooseGroup").setAttribute("class", "modal fade in");
    document.getElementById("chooseGroup").setAttribute("style", "display: block; padding-right: 17px;");

}

function OnCloseGroup(name) {
    document.getElementById(name).setAttribute("class", "modal fade");
    document.getElementById(name).setAttribute("style", "display: none;");
}

function OnConfirmGroup(name) {
    let index = document.getElementById("groupOption").selectedIndex;
    //alert("你选择了小组" + groupInfo[index][0] + "(" + groupInfo[index][1] + ")，保存功能未实现OvO");
    var sentMessage = {
        "task_id": deputeID,
        "task_department": partName[index][1]
    };
    $.ajax({
        url: '/task/allot',
        type: 'post',
        data: sentMessage,
        async: false,
        success: function (data, status) {
            if (status == 'success') {
                alert("分配完成");
            }
        },
        error: function (data, status) {
            if (status == 'error') {
                alert("你的网络有问题！");
            }
        }
    });
    OnCloseGroup(name);
    history.back();
}